﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.SqlClient;

namespace LI4.Data
{
    class Area
    {
        private Ligacao ligacao;

        public Area()
        {
            ligacao = new Ligacao();
        }

        public void Inserir(Business.Area area)
        {
            if (!existeArea(area.Nome))
                ligacao.Update("INSERT [dbo].[Area] ( [nome] ) VALUES (N'" + area.Nome + "')");
        }

        public void Alterar(Area c)
        {

        }
        public void Remover(Area c)
        {

        }

        public ArrayList getAreas()
        {
            ArrayList array = new ArrayList();
            SqlDataReader reader;

            ligacao.Select("SELECT cod_area,nome FROM Area ORDER BY nome");
            reader = ligacao.mySqlDataReader;

            while (reader.Read())
                array.Add(new Business.Area(reader.GetInt32(0), reader.GetString(1)));

            reader.Close();
            return array;
        }

        public bool existeArea(string nome)
        {
            SqlDataReader reader;

            ligacao.Select("SELECT nome FROM Area where nome='" + nome + "';");
            reader = ligacao.mySqlDataReader;
            if (reader.Read())
            {
                reader.Close();
                return true;
            }
            else
            {
                reader.Close();
                return false;
            }
        }

        public Business.Area getArea(int codigo)
        {
            SqlDataReader reader;
            Data.Distrito distData = new Data.Distrito();

            ligacao.Select("SELECT nome FROM Area where cod_area='" + codigo + "'");
            reader = ligacao.mySqlDataReader;
            reader.Read();
            string nome = reader.GetString(0);
            reader.Close();


            Business.Area area = new Business.Area(codigo, nome);

            return area;
        }

        public ArrayList getAreasCod()
        {

            ArrayList array = new ArrayList();

            SqlDataReader reader;

            ligacao.Select("SELECT cod_area,nome FROM Area ORDER BY cod_area");

            reader = ligacao.mySqlDataReader;

            while (reader.Read())

                array.Add(new Business.Area(reader.GetInt32(0), reader.GetString(1)));



            reader.Close();

            return array;

        }

        public Business.Area getAreaNome(string nomep)
        {
            SqlDataReader reader;
            Data.Distrito distData = new Data.Distrito();
            Business.Area area = new Business.Area(); ;

            ligacao.Select("SELECT * FROM Area where nome ='" + nomep + "';");
            reader = ligacao.mySqlDataReader;
            if (reader.Read())
            {
                string nome = reader.GetString(1);
                int codigo = reader.GetInt32(0);

                reader.Close();

                area = new Business.Area(codigo, nome);
            }

            reader.Close();

            return area;
        }

        public ArrayList getAreasNome(string nomep)
        {
            SqlDataReader reader;
            ArrayList array = new ArrayList();
            Data.Distrito distData = new Data.Distrito();

            ligacao.Select("SELECT * FROM Area where nome='" + nomep + "';");
            reader = ligacao.mySqlDataReader;
            while (reader.Read())
            {
                string nome = reader.GetString(1);
                int codigo = reader.GetInt32(0);

                Business.Area area = new Business.Area(codigo, nome);
                array.Add(area);
            }
            reader.Close();
            return array;
        }
    }
}
